package com.rogo.rogobox.models
{
	import com.rogo.mvc.sub.proxy.Proxy;
	import com.rogo.rogobox.App;
	import com.rogo.rogobox.AppFacade;
	import com.sina.microblog.data.MicroBlogUser;
	import com.sina.microblog.events.MicroBlogErrorEvent;
	import com.sina.microblog.events.MicroBlogEvent;
	
	import flash.events.SecurityErrorEvent;
	
	/**
	 * 1.10 账号接口
	 * 
	 * @see http://open.weibo.com/wiki/API文档
	 * 
	 * @author rogo
	 */	
	public class AccountProxy extends Proxy
	{
		public function AccountProxy()
		{
			super(NAME);
			
			init();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'accountProxy';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var blog:BlogDelegate;
		
		//-------------
		//	user
		//-------------
		private var _user:MicroBlogUser;

		/**
		 * 用户信息 
		 */
		public function get user():MicroBlogUser
		{
			return _user;
		}

		/**
		 * @private
		 */
		public function set user(value:MicroBlogUser):void
		{
			_user = value;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		private function init():void
		{
			blog = BlogDelegate.getInstance();
			blog.addEventListener(MicroBlogEvent.VERIFY_CREDENTIALS_RESULT, blog_verifyResultHandler);
			blog.addEventListener(MicroBlogErrorEvent.OAUTH_CERTIFICATE_ERROR, blog_verifyErrorHandler);
			blog.addEventListener(MicroBlogErrorEvent.VERIFY_CREDENTIALS_ERROR, blog_verifyErrorHandler);
			blog.addEventListener(SecurityErrorEvent.SECURITY_ERROR, blog_securityErrorHandler);
		}

		/**
		 * 登录(暂只支持PIN)
		 */		
		public function login():void
		{
			blog.login(null, null, true);
		}
		
		/**
		 * 使用PIN码认证 
		 */		
		public function verify(pin:String):void
		{
			if (!blog.oauthLoader)
			{
				sendNotification(AppFacade.SHOW_TIP, '请获取授权码');
			}
			else
			{
				sendNotification(AppFacade.LOADING_START);
				blog.pin = pin;
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function blog_verifyResultHandler(e:MicroBlogEvent):void
		{
			sendNotification(AppFacade.LOADING_END);
			user = e.result as MicroBlogUser;
			App.getInstance().verifyUser = user;
			App.getInstance().mainPageUser = user;
			App.getInstance().profilePageUser = user;
			App.getInstance().friendPageUser = user;
			sendNotification(AppFacade.LOGIN_RESULT, user);
		}
		
		private function blog_verifyErrorHandler(e:MicroBlogErrorEvent):void
		{
			sendNotification(AppFacade.LOGIN_FAIL);
			sendNotification(AppFacade.LOADING_END);
		}
		
		private function blog_securityErrorHandler(e:SecurityErrorEvent):void
		{
			sendNotification(AppFacade.LOGIN_FAIL);
			sendNotification(AppFacade.LOADING_END);
		}
		
	}
}